package org.zjt.clone;

import lombok.*;
import org.zjt.thread.Try;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * Description: Object -> String ： valueOf 方法
 *
 *              string常量字符串 -> String Builder
 *
 * @author juntao.zhang
 * Date: 2018-06-05 上午10:21
 * @see
 */
@Builder
@EqualsAndHashCode  // 实现属性的深层equals比较 、 hash覆盖
public class Team implements Cloneable {

    private String NO;

    private List<User> userList;


    /**
     *  深拷贝
     *
     * @return
     * @throws CloneNotSupportedException
     */
    @Override
    public Team clone() throws CloneNotSupportedException {
        Team team = (Team) super.clone();
        if (Objects.nonNull(this.userList) )
            team.userList = this.userList.stream().map( Try.of(User::clone) ).collect(Collectors.toList());
        return team;
    }

    /**
     * 必须重写 tostring 方法。对象转字符串使用 String.valueOf
     * @return
     */
    @Override
    public String toString() {
        return "Team{" +
                "NO='" + NO + '\'' +
                ", userList=" + userList +
                '}';
    }

    public static void main(String[] args) {
        Team team =  Team.builder().NO("ssssss").build();
        team.toString();
        System.out.println(team);
    }
}

@Data
@NoArgsConstructor
@AllArgsConstructor
class User implements Cloneable{

    private String name;

    private Integer age;

    @Override
    public User clone() throws CloneNotSupportedException {
        return (User)super.clone();
    }
}